/**
 * 新增走访数据(全新)
 * Created by xc on 2015/6/11.
 */
define(function (require, exports, module) {
  'use strict';
  module.exports = function (app) {
    app.register.controller('InterviewAddCtrl', InterviewAdd);

    InterviewAdd.$inject = ['$scope', '$filter', 'MHService', '$location', 'Upload'];
    function InterviewAdd($scope, $filter, MHService, $location, Upload) {
      $scope.building = {
        marketer: '',
        estateName: '',
        estateId: ''
      };
      /**
       * 监听输入框
       */
      $scope.$watch('search', function () {
        //name变化时
        if ($scope.search && $scope.search.length > 0) {
          MHService.postReq({url: MHService.URL.search_by_name + '?key=' + $scope.search}, function (data) {
            $scope.feeds = data['datas'];
          });
        }
      }, true);
      /**
       * 选中的楼盘
       * @param feed
       */
      $scope.feedSlected = function (feed) {
        $scope.feeds = undefined;
        $scope.search = undefined;
        $scope.building.estateName = feed[1];
        $scope.building.estateId = feed[0];
      };
      /**
       * 营销负责人
       */
      MHService.postReq({url: MHService.URL.search_marketer}, function (data) {
        $scope.marketers = data['datas'];
        $scope.selected = data['datas'][0];
        $scope.building.marketer = $scope.selected['id'];
      });
      /**
       * 提交数据
       * @param data
       */
      $scope.visitData = function (_data) {
        //时间格式化处理
        var data = angular.copy(_data);
        data.visitDate = $filter('date')(_data.visitDate, 'yyyy-MM-dd');
        data.baseInfotime = $filter('date')(_data.baseInfotime, 'yyyy-MM-dd');
        data.jointtime = $filter('date')(_data.jointtime, 'yyyy-MM-dd');
        data.planfinishtime = $filter('date')(_data.planfinishtime, 'yyyy-MM-dd');
        data.extension = $filter('date')(_data.extension, 'yyyy-MM-dd');
        data.marketer = $scope.selected['id'];
        MHService.postReq({url: MHService.URL.visit_add, _req: data}, function (data) {
          if (data['success']) {
            MHService.Toast('提交成功');
          } else {
            MHService.Toast('操作失败，请刷新重试');
          }
        });
      };
      /**
       * 推广方案
       */
      $scope.fangAn = function ($files) {
        console.log($files);
        if ($files.length > 0) {
          Upload.upload({url: MHService.URL.img_upload, method: 'POST', file: $files[0]}).success(function (res) {
            //文件上传完成
            console.log(res);
            alert(JSON.stringify(res.data));
            $scope.building.extensionplan = res.data;
          });
        }
      };
      /**
       * 平面图
       * @param $files
       */
      $scope.pingMian = function ($files) {
        console.log($files);
        if ($files.length > 0) {
          Upload.upload({url: MHService.URL.img_upload, method: 'POST', file: $files[0]}).success(function (res) {
            //文件上传完成
            alert(JSON.stringify(res.data));
            $scope.building.plane = res.data;
          });
        }
      };
    }
  }
});
